import unittest
import ddt
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.firefox.service import Service

dp1 = [
    ('hanamaki', '123456'),
    ('andy', '123456'),
    ('rose', '123456')
]


@ddt.ddt()
class EcShopShoppinUnitTest(unittest.TestCase):
    driver = None

    @classmethod
    def setUpClass(cls) -> None:
        service = Service(executable_path='../../dirver_exe/geckodriver.exe')
        cls.driver = webdriver.Firefox(service=service)

        # 隐式等待
        cls.driver.implicitly_wait(15)

    @ddt.data(*dp1)
    def test_shopp(self, data):
        self.driver.get("http://192.168.0.109/ecshop/")
        self.driver.maximize_window()
        if self.driver.page_source.__contains__('退出'):
            self.driver.find_element(By.XPATH, "/html/body/div[1]/div[2]/ul/li[1]/font/font/a[2]").click()
            self.shipping(data)
        else:
            self.shipping(data)

    @classmethod
    def tearDownClass(cls) -> None:
        cls.driver.quit()

    def shipping(self, data):
        self.driver.find_element(By.CSS_SELECTOR, "#ECS_MEMBERZONE > a:nth-child(2) > img").click()
        self.driver.find_element(By.NAME, "username").send_keys(data[0])
        self.driver.find_element(By.NAME, "password").send_keys(data[1])
        self.driver.find_element(By.NAME, "submit").click()

        self.driver.find_element(By.CSS_SELECTOR, "#show_best_area > .goodsItem:nth-child(1) .goodsimg").click()
        self.driver.find_element(By.CSS_SELECTOR, ".padd > a:nth-child(1) > img").click()

        self.driver.find_element(By.XPATH, "/html/body/div[7]/div[1]/table/tbody/tr/td[2]/a/img").click()  # 结算中心

        if self.driver.page_source.__contains__('配送至这个地址'):
            self.driver.find_element(By.XPATH, "//option[@value='25']").click()
            self.driver.find_element(By.XPATH, "//option[@value='321']").click()
            self.driver.find_element(By.XPATH, "//option[. = '宝山区']").click()
            self.driver.find_element(By.ID, "consignee_0").send_keys(data[0])
            self.driver.find_element(By.ID, "email_0").send_keys("123@qq.com")
            self.driver.find_element(By.ID, "address_0").send_keys("智慧湾")
            self.driver.find_element(By.ID, "zipcode_0").send_keys("220000")
            self.driver.find_element(By.ID, "tel_0").send_keys("15689898978")
            self.driver.find_element(By.ID, "mobile_0").send_keys("15689898978")
            self.driver.find_element(By.ID, "sign_building_0").send_keys("桥")
            self.driver.find_element(By.ID, "best_time_0").send_keys("现在")
            self.driver.find_element(By.NAME, "Submit").click()
        else:

            self.driver.find_element(By.NAME, "shipping").click()
            self.driver.find_element(By.XPATH, "/html/body/div[7]/form/div[7]/table/tbody/tr[3]/td[1]/input").click()
            self.driver.find_element(By.XPATH, "/html/body/div[7]/form/div[15]/div[2]/input[1]").click()

        self.assertTrue(self.driver.page_source.__contains__('感谢您在本店购物！您的订单已提交成功，请记住您的订单号'))


if __name__ == '__main__':
    unittest.main()
